package com.dongzhu1428.express.module1.rest;

import com.dongzhu1428.express.module1.Result;
import com.dongzhu1428.express.module1.dto.AddUserParamDto;
import com.dongzhu1428.express.module1.dto.controllerDto.ResignToBeDeliverymanParams;
import com.dongzhu1428.express.module1.dto.controllerDto.User.GetDeliverymanInfo;
import com.dongzhu1428.express.module1.entity.User;
import com.dongzhu1428.express.module1.mapper.express.UserMapper;
import com.dongzhu1428.express.module1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户模块
 */
@RestController
@RequestMapping("user")
public class UserController {
	@Autowired
	UserMapper userMapper;

	@Autowired
	UserService userService;



	/**
	 * 获取用户列表
	 * @return
	 */
	@GetMapping("getUserList")
	public Result<List<User>>  getUserList(){
		List<User> users = userMapper.selectList(null);
		return Result.success(users);
	}

	/**
	 * 添加用户
	 * @param dto
	 * @return
	 */
	@PostMapping("addUser")
	public Result<String> addUser(@RequestBody AddUserParamDto dto){
                 userMapper.insert(User.builder()
				                 .name(dto.getName())
				                 .password(dto.getPassword())
				                 .phone(dto.getPhone())
				                 .email(dto.getEmail())
				                 .nickname(dto.getNickname())
		                         .build());
				 return  Result.success("添加用户成功");
	}


	/**
	 * 获取指定用户信息
	 * @param id
	 * @return
	 */
	@GetMapping("getUserInfo")
	public Result<User> getUserInfo(@RequestParam int id){
		User user = userMapper.selectById(id);
		return  Result.success(user);
	}


	/**
	 * 判断是否为快递员
	 */
	@GetMapping("isDeliveryman")
	public Result<Boolean> idDeliveryman(int userId){
		return userService.isDeliveryman(userId);
	}

	/**
	 * 注册成为送货人
	 *
	 * @param dto dto
	 * @return {@link Result}<{@link String}>
	 */
	@PostMapping("registerToBeDeliveryman")
	public Result<String> registerToBeDeliveryman(ResignToBeDeliverymanParams dto){
		return userService.registerToBeDeliveryman(dto);
	}

	/**
	 * 获取送货人信息
	 *
	 * @param id 快递员id
	 * @return {@link Result}<{@link GetDeliverymanInfo}>
	 */
	@GetMapping("getDeliverymanInfo")
	public  Result<GetDeliverymanInfo> getDeliverymanInfo(int id){
		return userService.getDeliverymanInfo(id);
	}


}
